package com.qdxwx.web.controller.order;

import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.account.UserAccountService;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.member.MemberSaleService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.user.UserAddressService;
import com.qdxwx.models.account.UserAccount;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentDetail;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.member.MemberSale;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserAddress;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Order接口
 * @author developer
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private MemberTypeService memberTypeService;
	@Autowired
	private MemberSaleService memberSaleService;
	@Autowired
	private UserAccountService userAccountService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private BookService bookService;
	@Autowired
	private UserAddressService userAddressService;
	@Autowired
	private MemberRecordService memberRecordService;
	@Autowired
	private DocumentService documentService;

	@RequestMapping("/course.html")
	public String buyCourse(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return toLoginPage();
		}
		int courseId = TKConvert.toInteger(request.getParameter("courseId"));
		Course course = courseService.get(courseId);
		if (course == null) {
			return toError404(request);
		}
		UserAccount userAccount = userAccountService.getByUserId(user.getUserId());
		if (userAccount == null) {
			return toError404(request);
		}
		mmap.put("user", user);
		mmap.put("userAccount", userAccount);
		mmap.put("course", course);
		return getViewPath("order/buy-course");
	}

	@RequestMapping("/book.html")
	public String buyBook(HttpServletRequest request, ModelMap mmap){
		User user = LoginUser.getUser(request);
		if(user == null){
			return toError404(request);
		}
		int bookId =TKConvert.toInteger(request.getParameter("bookId"));
		Book book = bookService.get(bookId);
		if(book == null){
			return toError404(request);
		}

		List<UserAddress> address = userAddressService.getList(user.getUserId(), 3);

		UserAccount userAccount = userAccountService.getByUserId(user.getUserId());
		if(userAccount == null){
			return toError404(request);
		}
		mmap.put("user",user);
		mmap.put("userAccount",userAccount);
		mmap.put("book",book);
		mmap.put("address",address);
		return getViewPath("order/buy-book");
	}

	@RequestMapping("/member.html")
	public String openMember(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return toError404(request);
		}
		UserAccount userAccount = userAccountService.getByUserId(user.getUserId());
		if (userAccount == null) {
			return toError404(request);
		}

		int typeId = TKConvert.toInteger(request.getParameter("typeId"), 1);
		MemberType memberType = memberTypeService.get(typeId);
		if (memberType == null) {
			return toError404(request);
		}
		List<MemberSale> memberSales = memberSaleService.getListByType(typeId);
		if (memberSales.size() < 1) {
			return toError404(request);
		}
		MemberSale defaultMember = memberSales.get(0);
		MemberRecord memberRecord = memberRecordService.getByUserId(user.getUserId(), memberType.getId());
		mmap.put("user", user);
		mmap.put("userAccount", userAccount);
		mmap.put("memberRecord", memberRecord);
		mmap.put("memberType", memberType);
		mmap.put("memberSales", memberSales);
		mmap.put("defaultMember", defaultMember);
		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("order/buy-vip");
		}
		return getViewPath("order/buy-member");
	}

	@RequestMapping("/document.html")
	public String buyDocument(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return toLoginPage();
		}
		int documentId = TKConvert.toInteger(request.getParameter("documentId"));
		Document document = documentService.get(documentId);
		if (document == null) {
			return toError404(request);
		}
		UserAccount userAccount = userAccountService.getByUserId(user.getUserId());
		if (userAccount == null) {
			return toError404(request);
		}
		mmap.put("user", user);
		mmap.put("userAccount", userAccount);
		mmap.put("document", document);
		return getViewPath("order/buy-document");
	}
}